package com.lzc.tlmail.tlmailproduct.service.impl;


import com.lzc.tlmail.tlmailproduct.dao.AttrDao;
import com.lzc.tlmail.tlmailproduct.entity.AttrAttrgroupRelationEntity;
import com.lzc.tlmail.tlmailproduct.entity.AttrEntity;
import com.lzc.tlmail.tlmailproduct.service.AttrService;
import com.lzc.tlmail.tlmailproduct.vo.AttrattrgrouprelationVo;
import com.lzc.tlmail.tlmailproduct.vo.AttrgroupwithattrVo;
import com.lzc.tlmail.tlmailproduct.vo.SkuItemVo;
import com.lzc.tlmail.tlmailproduct.vo.SpuBaseGroupAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.Query;

import com.lzc.tlmail.tlmailproduct.dao.AttrGroupDao;
import com.lzc.tlmail.tlmailproduct.entity.AttrGroupEntity;
import com.lzc.tlmail.tlmailproduct.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrGroupDao attrGroupDao;

    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据三级分类查询分组
     * @param params
     * @param cateid
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer cateid) {
        if(cateid==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>());
            return new PageUtils(page);
        }else {
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper=new QueryWrapper<AttrGroupEntity>().eq("catelog_id",cateid);
            if(!StringUtils.isEmpty(key)) {
                queryWrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return new PageUtils(page);
        }

    }

    /**
     * 删除属性分组关联的属性
     * @param attrattrgrouprelationVo
     */
    @Override
    public void removerelation(AttrattrgrouprelationVo[] attrattrgrouprelationVo) {

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = Arrays.asList(attrattrgrouprelationVo).stream().map((attrAttrgroupRelationvo) -> {
            AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrAttrgroupRelationvo, attrgroupRelationEntity);
            return attrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrGroupDao.removeRelation(attrAttrgroupRelationEntities);
    }

    /**
     * 根据分类id查询分组和分组的属性
     * @param catId
     * @return
     */
    @Override
    public List<AttrgroupwithattrVo> getAttrGroupAndAttrByCatId(Long catId) {
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        List<AttrgroupwithattrVo> attrgroupwithattrVos = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrgroupwithattrVo attrgroupwithattrVo = new AttrgroupwithattrVo();
            BeanUtils.copyProperties(attrGroupEntity, attrgroupwithattrVo);
            List<AttrEntity> attrEntities = attrService.attrServicegetAttrById(attrgroupwithattrVo.getAttrGroupId());
            attrgroupwithattrVo.setAttrs(attrEntities);
            return attrgroupwithattrVo;
        }).collect(Collectors.toList());
        return attrgroupwithattrVos;
    }

    /**
     * 根据spuid的到分组和分组属性的信息
     * @param spuId
     * @param catalogId
     * @return
     */
    @Override
    public List<SpuBaseGroupAttrVo> getAttrGroupAndAttrBySpuId(Long spuId, Long catalogId) {
        AttrGroupDao baseMapper = this.getBaseMapper();
        //得到分组属性信息
        List<SpuBaseGroupAttrVo> vos=baseMapper.getAttrGroupAndAttrBySpuId(spuId,catalogId);
        return vos;
    }

}